package cn.itsource.pethome.system.pro.service.impl;

import cn.itsource.pethome.jwt.LoginContextUtils;
import cn.itsource.pethome.system.pet.domain.Pet;
import cn.itsource.pethome.system.pro.domain.Product;
import cn.itsource.pethome.system.pro.domain.ProductDetail;
import cn.itsource.pethome.system.pro.mapper.ProductDetailMapper;
import cn.itsource.pethome.system.pro.mapper.ProductMapper;
import cn.itsource.pethome.system.pro.service.IProductService;
import cn.itsource.pethome.base.service.impl.IBaseServiceImpl;
import cn.itsource.pethome.system.user.mapper.EmployeeMapper;
import cn.itsource.pethome.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xlh
 * @since 2023-02-09
 */
@Service
public class ProductServiceImpl extends IBaseServiceImpl<Product> implements IProductService {
    @Autowired
    private LoginContextUtils loginContextUtils;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Override
    public void add(Product product, HttpServletRequest request) {
        product.setState(0l);
        product.setOffsaletime(new Date());
        super.add(product);
        /*保存详细信息*/
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.add(detail);

    }
/*上架和下架*/
    @Override
    public void onOrOffSale(int flag, List<Long> ids) {
        if (flag == 1) {
            //修改上架状态及时间
            Map<String, Object> params = new HashMap<>();
            params.put("onsaleTime", new Date());
            params.put("ids", ids);
            productMapper.onsale(params);
        } else {
            //修改下架状态及时间
            Map<String, Object> params = new HashMap<>();
            params.put("offsaleTime", new Date());
            params.put("ids", ids);
            productMapper.offSale(params);
        }
    }

    @Override
    public void update(Product product) {
        //根据product.getId 查询详情整条数据
        ProductDetail xixi = productDetailMapper.xixi(product.getId());
        //获取到详情的id
        Long id = xixi.getId();
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        detail.setId(id);
        super.update(product);
        productDetailMapper.update(detail)
        /*修改详细信息*/
        /*ProductDetail detail = product.getDetail();
        System.out.println(detail);
        detail.setProductId(product.getId());
        productDetailMapper.update(detail)*/;
    }

    @Override
    public void delete(Long id) {
        Product findnoe = productMapper.findnoe(id);
        Long state = findnoe.getState();
        if (state==0){
            productDetailMapper.deleteByProductId(id);
            super.delete(id);
        }else {
            throw new BusinessException("上架不能删除！！！");
        }


    }
}
